package com.jn.erp.wechat.common.api.service.impl;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.security.entity.SysUser;
import com.jn.erp.security.sysservice.SysUserService;
//import com.jn.erp.web.api.student.entity.StudentUser;
//import com.jn.erp.web.api.student.service.StudentUserService;
import com.jn.erp.wechat.common.api.entity.WxAccess;
import com.jn.erp.wechat.common.api.entity.dto.WxMaLoginDto;
import com.jn.erp.wechat.common.api.mapper.WxAccessMapper;
import com.jn.erp.wechat.common.api.service.WxAccessService;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/8/28 15:45
 */
@Service
public class WxAccessServiceImpl extends ServiceImpl<WxAccessMapper, WxAccess> implements WxAccessService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public WxAccess getOrSaveRecord(WxOAuth2UserInfo user, String from) {
        WxAccess access = getByOpenId(user.getOpenid());
        if (access == null) {
            access = new WxAccess();
            access.setOpenid(user.getOpenid());
            access.setUnionid(user.getUnionId());
            access.setNickname(user.getNickname());
            access.setAddTime(LocalDateTime.now());
            access.setLatestLogin(LocalDateTime.now());
            access.setUnsubscribe(false);
            access.setHeadImg(user.getHeadImgUrl());
            access.setGender( user.getSex());
            access.setCity(user.getCity());
            access.setProvince(user.getProvince());
            access.setCountry(user.getCountry());
            access.setRegFrom(from);
            save(access);
        } else {
            access.setNickname(user.getNickname());
            access.setHeadImg(user.getHeadImgUrl());
            access.setGender( user.getSex());
            access.setCity(user.getCity());
            access.setProvince(user.getProvince());
            access.setCountry(user.getCountry());
            access.setLatestLogin(LocalDateTime.now());
            access.setRegFrom(from);
            updateById(access);
        }
        return access;
    }


    @Override
    public WxAccess getByOpenId(String openid) {
        QueryWrapper<WxAccess> qw = new QueryWrapper<>();
        qw.eq("openid", openid);
        return getOne(qw);
    }

    //根据openId获取教师员工信息
    @Override
    public SysUser getByWxAccessId(Long wxAccessId) {
        QueryWrapper<SysUser> qw=new QueryWrapper<>();
        qw.eq("wx_access_id",wxAccessId);
        qw.eq("state", 0);
        return sysUserService.getOne(qw);
    }


    //记录并更新小程序登录用户信息
    @Override
    public WxAccess logMpRecord(WxMaJscode2SessionResult session , WxMaLoginDto loginDto, String from) {
        WxOAuth2UserInfo userInfo = new WxOAuth2UserInfo();
        userInfo.setOpenid(session.getOpenid());
        userInfo.setUnionId(session.getUnionid());
        userInfo.setNickname(loginDto.getNickName());
        userInfo.setSex(loginDto.getGender());
        userInfo.setCity(loginDto.getCity());
        userInfo.setProvince(loginDto.getProvince());
        userInfo.setCountry(loginDto.getCountry());
        userInfo.setHeadImgUrl(loginDto.getAvatarUrl());
        WxAccess wxAccess = getOrSaveRecord(userInfo, from);
        return wxAccess;
    }
}
